package com.sanda.androidcourse.chapter06;

import android.content.Context;
import android.view.View;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class FileHelper {

    /**
     * 从内部存储写文件内容
     *
     * @param context
     * @param filename
     * @param content
     */

    public static void writeFileToInternalStorage(Context context, String filename, String content) {
        try {
            // 获取应用的内部存储目录
            FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
            fos.write(content.getBytes());
            fos.close();
            Toast.makeText(context, "文件成功存储", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从内部存储读取文件内容
     *
     * @param context
     * @param filename
     * @return
     */
    public static String readFileFromInternalStorage(Context context, String filename) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            String filePath = context.getFilesDir() + "/" + filename;
            // 读取文件并使用 UTF-8 编码
            List<String> lines = Files.readAllLines(Paths.get(filePath), java.nio.charset.StandardCharsets.UTF_8);

            // 将读取的内容拼接成一个字符串
            for (String line : lines) {
                stringBuilder.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 从外部存储写文件内容
     * @param context
     * @param filename
     * @param content
     */
    public static void writeFileToExternalStorage(Context context, String filename, String content) {
        try {
            // 获取外部存储的私有文件目录
            FileOutputStream fos = new FileOutputStream(new File(context.getExternalFilesDir(null), filename));
            fos.write(content.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从外部存储读取文件内容
     * @param context
     * @param filename
     * @return
     */
    public static String readFileFromExternalStorage(Context context, String filename) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            // 获取外部存储的私有目录文件
            FileInputStream fis = new FileInputStream(new File(context.getExternalFilesDir(null), filename));
            // 使用 InputStreamReader 并指定编码格式为 UTF-8
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader reader = new BufferedReader(isr);

            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

}
